package com.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.JsonResponse;
import com.mapper.DocumentDetailMapper;
import com.mapper.DocumentInfoMapper;
import com.model.DocumentDetail;
import com.model.DocumentInfo;
import com.service.DataService;
import com.vo.*;
import javafx.util.Pair;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
public class DataController {
    @Autowired
    DataService dataService;

    @Autowired
    DocumentDetailMapper documentDetailMapper;

    @Autowired
    DocumentInfoMapper documentInfoMapper;

    @GetMapping("/dayStatics")
    public JsonResponse dayStatics(@Param("startTime") String startTime, @Param("endTime") String endTime){
        List<DayCountVo> list = dataService.groupByDay(startTime, endTime);
        return JsonResponse.success(list, "日期分组");
    }

    @GetMapping("/dayCollegeStatics")
    public JsonResponse dayCollegeStatics(@Param("startTime") String startTime, @Param("endTime") String endTime){
        List<DayCollegeCountVo> list = dataService.groupByDayAndCollege(startTime, endTime);
        return JsonResponse.success(list, "日期学院分组");
    }

    @GetMapping("/collegeStatics")
    public JsonResponse collegeStatics(@Param("startTime") String startTime, @Param("endTime") String endTime){
        List<CollegeCountVo> list = dataService.groupByCollege(startTime, endTime);
        return JsonResponse.success(list, "学院分组");
    }

    /**
     * 获取详细信息
     */
    @PostMapping("/getDetailList")
    public JsonResponse getDetailList(@RequestBody DetailListConditionVo detailListConditionVo){
        Page page = new Page<>();
        page.setCurrent(detailListConditionVo.getPage());
        page.setSize(detailListConditionVo.getPageSize());
        QueryWrapper<DocumentDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("search_keyword", detailListConditionVo.getType());
        queryWrapper.like("keywords", detailListConditionVo.getKeywords());
        documentDetailMapper.selectPage(page, queryWrapper);
        List records = page.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("list", records);
        data.put("page", page.getCurrent());
        data.put("pageSize", page.getSize());
        data.put("total", page.getTotal());
        return JsonResponse.success(data);
    }

    /**
     * 获取各个时间段词云信息
     */
    @PostMapping("/wordCloudList")
    public JsonResponse getCloudList(@RequestBody DetailListConditionVo detailListConditionVo){
        System.out.println("word cloud: " + detailListConditionVo);
        TreeMap<String, Integer> map = new TreeMap<>();
//        QueryWrapper<DocumentDetail> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("search_keyword", detailListConditionVo.getType());
//        queryWrapper.ge("Date(release_time)", "Date(" + detailListConditionVo.getStartTime() + ")");
//        queryWrapper.le("Date(release_time)", "Date(" + detailListConditionVo.getEndTime() + ")");
//        List<DocumentDetail> list = documentDetailMapper.selectList(queryWrapper);
        List<DocumentDetail> list = documentDetailMapper.query(detailListConditionVo.getStartTime(), detailListConditionVo.getEndTime(), detailListConditionVo.getType());
        System.out.println("size: " + list.size());
        list.stream().forEach(item->{
            String keywords = item.getKeywords();
            String[] split = keywords.split(";");
            for (String keyword : split){
                keyword = keyword.trim();
                if(!StringUtils.isEmpty(keyword)){
                    if(map.containsKey(keyword)){
                        map.put(keyword, map.get(keyword) + 1);
                    }else{
                        map.put(keyword, 1);
                    }
                }
            }
        });
        ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
        Collections.sort(entries, (o1, o2)->{
            return -1 * o1.getValue().compareTo(o2.getValue());
        });

        List<Map.Entry<String, Integer>> list1 = entries.subList(0, Math.min(entries.size(), 100));
        List<Pair> res = list1.stream().map(o -> {
            return new Pair(o.getKey(), o.getValue());
        }).collect(Collectors.toList());
        return JsonResponse.success(res);
    }

    /**
     * 按院校统计排名前十的论文数
     */
    @GetMapping("/collegeTop")
    public JsonResponse getTopCollege(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("type") String type){
        List<SourceNumVo> sourceNumVos = documentInfoMapper.querySourceTop(startTime, endTime, type);
        return JsonResponse.success(sourceNumVos);
    }

    /**
     * 按数据抓取类别统计总数
     */
    @GetMapping("/typeCount")
    public JsonResponse getTypeCount(@Param("startTime") String startTime, @Param("endTime") String endTime){
        List<SourceNumVo> list = documentInfoMapper.queryTypeTop(startTime, endTime);
        return JsonResponse.success(list);
    }

    /**
     * 前100名下载次数
     */
    @GetMapping("/downloadTop")
    public JsonResponse getTopDownload(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("type") String type){
        List<DocumentInfo> sourceNumVos = documentInfoMapper.queryDownloadTop(startTime, endTime, type);
        return JsonResponse.success(sourceNumVos);
    }
}
